/**
 * 
 */
package hoan.nguyen.webapp.controller;

import hoan.nguyen.dao.AddressDAO;
import hoan.nguyen.dao.EmployeeDAO;
import hoan.nguyen.dao.LevelDAO;
import hoan.nguyen.domain.Address;
import hoan.nguyen.domain.Employee;
import hoan.nguyen.domain.Level;
import hoan.nguyen.service.EmployeeService;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import util.JSONUtils;

/**
 * @author Horizon
 * 
 */
@Controller
public class EmployeeController {

	private EmployeeDAO employeeDAO;
	private AddressDAO addressDAO;
	private LevelDAO levelDAO;
	private EmployeeService employeeService;

	/**
	 * @param employeeDAO
	 *            the employeeDAO to set
	 */
	@Autowired
	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	/**
	 * @param addressDAO
	 *            the addressDAO to set
	 */
	@Autowired
	public void setAddressDAO(AddressDAO addressDAO) {
		this.addressDAO = addressDAO;
	}

	/**
	 * @param levelDAO
	 *            the levelDAO to set
	 */
	@Autowired
	public void setLevelDAO(LevelDAO levelDAO) {
		this.levelDAO = levelDAO;
	}

	/**
	 * @param employeeService the employeeService to set
	 */
	@Autowired
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getAllEmployees", method = { RequestMethod.GET }, produces="application/json")
	public List<Employee> getAllEmplyees() {

		Address a = new Address();
		a.setId(1);
		a.setName("abc");

		String employeeListJson = employeeService.doGetRequest("http://localhost:8080/Y/services/getAllEmployees", "");

		return JSONUtils.convertJSONToObject(List.class, employeeListJson) ;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addAllEmployees", method = { RequestMethod.GET }, produces="application/json")
	public List<Employee> addAllEmplyees() {
		
		Address a = new Address();
		a.setId(1);
		a.setName("abc");
		
		String abc = JSONUtils.convertObjectToJSON(a);
		
		String employeeListJson = employeeService.doPostRequest("http://localhost:8080/Y/services/addAllEmployees", abc);
		
		return JSONUtils.convertJSONToObject(List.class, employeeListJson) ;
	}

	/**
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addEmployee", method = { RequestMethod.GET })
	public String loadAddEmpoyeePage(ModelMap map) {

		map.put("employee", new Employee());

		return "add-employee";
	}

	/**
	 * 
	 * @param employee
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addEmployee", method={RequestMethod.POST})
	public String addNewEmployee(@ModelAttribute Employee employee,
			@RequestParam(value = "address") String strAddress, @RequestParam(value = "level") String strLevel,
			ModelMap map) {
		
		Level level = new Level();
		level.setName(strLevel);
		level.setDes(strLevel);
		
		employee.getLevels().add(level);

		Employee addedEmployee = employeeDAO.save(employee);
		map.put("employee", addedEmployee);
		
		Employee e = employeeDAO.getById(addedEmployee.getId());
        
		return "add-employee";
	}
}
